#include "systray.h"
#include <QMenu>
#include <stdio.h>

systray::systray(QWidget *parent) :
    QWidget(parent)
{
    sets=new QSettings("Casa de putes","Qronos");
    QString dir=QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    log=sets->value("Fitxer").toString();
    time= new QTimer(this);
    connect(time,SIGNAL(timeout()),this,SLOT(min()));
    sys = new QSystemTrayIcon(this);
    sys->setIcon(QIcon(":cronos/icono"));
    context = new QMenu("Cronos",this);
    start = new QAction("Start",this);
    stop = new QAction("Stop",this);
    exit = new QAction("Cerrar",this);
    nomtarea = new QAction("Tarea a realizar...",this);

    connect(exit,SIGNAL(triggered()),this,SLOT(close()));
    connect(start,SIGNAL(triggered()),this,SLOT(empiezacuenta()));
    connect(stop,SIGNAL(triggered()),this,SLOT(fincuenta()));
    connect(nomtarea,SIGNAL(triggered()),this,SLOT(setlog()));
    context->addAction(nomtarea);
    context->addSeparator();
    context->addAction(start);
    context->addAction(stop);
    context->addSeparator();
    context->addAction(exit);
    sys->setContextMenu(context);
    start->setEnabled(true);
    stop->setEnabled(false);
    modo=PARADO;


    minuts=sets->value("Minuts",0).toInt();
    if(minuts>0){
       setlog(log);
       fincuenta();
    }
}

systray::~systray(){
    if(modo==MIDIENDO)fincuenta();
    delete context;
    delete start;
    delete stop;
    delete exit;
    delete nomtarea;
    delete time;
    delete sys;
}

void systray::showsys(){
    sys->show();
}

void systray::setlog(){
    log="";
    QString dir=sets->value("Fitxer",".").toString();
    log=QFileDialog::getSaveFileName(this,tr("Selecciona una tarea"),dir,tr("Log (*.log)"));
    if(log=="")return;
    if(!log.endsWith(".log"))log.append(".log");
    QFile hist(log);
    sets->setValue("Fitxer",hist.fileName());
    if(!hist.exists()){
        hist.open(QIODevice::WriteOnly);
        QTextStream out(&hist);
        out << trUtf8("Resúmenes de tiempo para la tarea ") << hist.fileName() << "\n";
    }
    sets->setValue("Fitxer", log);
}

void systray::setlog(QString l){
    log=l;
    QFile hist(log);
    if(!hist.exists()){
        hist.open(QIODevice::WriteOnly);
        QTextStream out(&hist);
        out << "Resúmenes de tiempo para la tarea " << hist.fileName() << "\n";
    }
}

void systray::empiezacuenta(){
    setlog();
    minuts=0;
    time->start(60000);
    start->setEnabled(false);
    stop->setEnabled(true);
    exit->setEnabled(false);
    inici=QDateTime::currentDateTime();
    inici=inici.addSecs(-inici.time().second());
    stop->setEnabled(true);
    modo=MIDIENDO;
}

void systray::fincuenta(){
    QFile temporal(log);
    temporal.open(QIODevice::WriteOnly | QIODevice::Append);
    QString line=inici.toString();
    line+="\t"+ QString::number(minuts,10) + " Minutos\n";
    QTextStream out(&temporal);
    out << line;
    sets->setValue("Minuts",0);
    minuts=0;
    time->stop();
    stop->setEnabled(false);
    start->setEnabled(true);
    exit->setEnabled(true);
    modo=PARADO;
}

void systray::min(){
    minuts++;
    sets->setValue("Minuts",minuts);
}

